<?php

// 热卖榜

namespace app\shop\controller;

use think\Request;
use think\Controller;
use app\shop\common\GuessGoods;
use app\shop\model\Config as ConfigModel;
use app\shop\model\ShopGoods as ShopGoodsModel;
use app\shop\model\GoodsType as GoodsTypeModel;

class HotSale extends Controller
{
    /**
     * 热卖榜列表
     * @param Request $request
     * @param ShopGoodsModel $shopGoods
     * @param ConfigModel $config
     * @param GoodsTypeModel $goodsType
     * @return mixed
     */
    public function index(Request $request, ShopGoodsModel $shopGoods, ConfigModel $config, GoodsTypeModel $goodsType)
    {
        //配置项
        $audit = $config->where('varname', 'web_commodity_audit')->value('varvalue');
        $condition = ($audit == 'Y') ? ['status' => 1, 'state' => 1] : ['state' => 1];
        $condition['state'] = ['eq',1];
        $condition['status'] = ['eq',1];
        // 获取参数
        $inquire = $request->param();
        if (!empty($inquire['pid'])) $condition['goods_str'] = ['like', '%,' . $inquire['pid'] . ',%'];

        // 商品分类
        $hot_sale_types = $goodsType->where(['parenTid' => 0, 'status' => 1])->field('id,title,ad_img')->order(['sort' => 'desc'])->select()->toArray();

        // 商品列表
        $goods_list = $shopGoods->where($condition)->order('sale_number desc')->field('id,thumb_img,state,status,price,rebate_percentage,title')->limit(13)->select()->toArray();

        // 猜你喜欢
        $guess_like = (new GuessGoods)->guessLike();


        return $this->fetch('', [
            'mate_head'     => '热卖榜',
            'guess_like'    => $guess_like,
            'hot_goods'     => $goods_list,
            'hot_sale_list' => $hot_sale_types,
        ]);

    }
}